package activity.dao;

import activity.entity.Activity;
import activity.utils.DBConn2;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class Actdao {
    public static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

//    显示活动信息
    public ArrayList<Activity>getActinfo(){
        DBConn2 obj=new DBConn2();
        Connection conn=obj.getConn();
        ArrayList<Activity>activities=new ArrayList<>();

        if (conn == null) {
            System.out.println("连接出错");
            return null;
        }
        String sql="select * from actable";

        try {
            PreparedStatement pstm=conn.prepareStatement(sql);
            ResultSet resultSet=pstm.executeQuery();
            while (resultSet.next()){
                Activity activity=new Activity();
                activity.setAname(resultSet.getString("aname"));
                activity.setAtime(resultSet.getString("atime"));
                activity.setAddress(resultSet.getString("address"));
                activity.setContactman(resultSet.getString("contactman"));
                activity.setCallnum(resultSet.getString("callnum"));
                activity.setActid(resultSet.getString("actid"));
                activities.add(activity);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            System.out.println("sql语句错了");
            return  null;
        }
        return activities;
    }

//    添加活动
    public boolean inseAct(Connection conn,Activity activity) throws SQLException {
        boolean flag=false;
        String sql="insert into actable (aname, atime, address, contactman, callnum,actid) values (?,?,?,?,?,?)";

        PreparedStatement pstm=conn.prepareStatement(sql);
        pstm.setString(1, activity.getAname());
        pstm.setString(2, activity.getAtime());
        pstm.setString(3, activity.getAddress());
        pstm.setString(4, activity.getContactman());
        pstm.setString(5,activity.getCallnum());
        pstm.setString(6, activity.getActid());

        int res=pstm.executeUpdate();
        System.out.println(res);
        if (res>0){
            flag=true;
        }
        return flag;
    }

//    删除活动
    public boolean delAct(Connection conn,Activity activity) throws SQLException {
        boolean flag=false;
        String sql = "delete from actable where actid=?";

        PreparedStatement pstm=conn.prepareStatement(sql);
        pstm.setString(1,activity.getActid());

        int res=pstm.executeUpdate();
        System.out.println("flag:"+res);
        if (res==0){
            flag=true;
        }
        return flag;
    }

//    修改活动
    public boolean upAct(Connection conn,Activity activity) throws SQLException {
        boolean flag=false;
        String sql="update actable set aname=?, atime=?, address=?, contactman=?, callnum=? where actid=?";
        PreparedStatement pstm=conn.prepareStatement(sql);
        pstm.setString(1, activity.getAname());
        pstm.setString(2, activity.getAtime());
        pstm.setString(3, activity.getAddress());
        pstm.setString(4, activity.getContactman());
        pstm.setString(5, activity.getCallnum());
        pstm.setString(6, activity.getActid());

        int res= pstm.executeUpdate();
        System.out.println(res);
        if (res>0){
            flag=true;
        }
        return flag;
    }

//    查询活动
public Activity selAct(Connection conn, Activity activity) throws SQLException {
        Activity resultAct=null;
        String sql="select * from actable where actid=?";

        PreparedStatement pstm=conn.prepareStatement(sql);
        pstm.setString(1, activity.getActid());

        ResultSet rs= pstm.executeQuery();
        if (rs.next()){
            resultAct=new Activity();
            resultAct.setAname(rs.getString("aname"));
            resultAct.setAtime(rs.getString("atime"));
            resultAct.setAddress(rs.getString("address"));
            resultAct.setContactman(rs.getString("contactman"));
            resultAct.setCallnum(rs.getString("callnum"));
            resultAct.setActid(rs.getString("actid"));
        }
        System.out.println("dao层获取的活动:"+resultAct);
        return resultAct;
}
}
